package com.ctrip.ec.api.admin.controller.member;

import com.ctrip.ec.api.admin.common.ValidateException;
import com.ctrip.ec.api.admin.model.query.QueryModel;

import com.ctrip.ec.biz.entity.brand.BrandEntity;
import com.ctrip.ec.biz.entity.role.RoleEntity;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.member.MemberDao;
import com.ctrip.ec.biz.entity.member.MemberEntity;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberDao member;

    @GetMapping("list")
    public List<MemberEntity> list() throws SQLException {
        List<MemberEntity> list = member.list();
        BrandEntity en = new BrandEntity();
        RoleEntity entity = new RoleEntity();
        return list;
    }

    @PostMapping("list")
    public List<MemberEntity> list(@RequestBody QueryModel model,
                                   @RequestParam(required = false) Boolean isMiniApp) throws SQLException {
        PagerSetting setting = model.build();
        if (isMiniApp != null && isMiniApp == true) {
            setting.appendWhere("agent_id>0");
        } else {
            setting.appendWhere("agent_id=" + RequestContext.getAgentId());
        }
        if (RequestContext.getAgentId() != null && RequestContext.getAgentId() > 0) {
            String where = " agent_id = " + RequestContext.getAgentId();
            if (!Strings.isNullOrEmpty(setting.getWhere())) {
                where = " and " + where;
            }
            setting.setWhere(setting.getWhere() + where);
        }
        List<MemberEntity> list = member.list(setting);
        RequestContext.setResponseHeaderCount(setting.getRecordCount());


        return list;
    }
    @PostMapping("miniAppList")
    public List<MemberEntity> minApplist(@RequestBody QueryModel model) throws SQLException {
        return this.list(model,true);
    }

    @GetMapping("detail/{id}")
    public MemberEntity detail(@PathVariable Long id) throws SQLException {
        return member.entity(id);
    }

    @PostMapping("save")
    public Long save(@RequestBody MemberEntity entity) throws SQLException {
        if (member.exist("mobile", entity.getMobile(), entity.getId())) {
            throw new ValidateException("手机号已经存在");
        }
        member.save(entity);
        return entity.getId();
    }

    @PostMapping("delete")
    public void delete(@RequestBody List<Long> ids) throws SQLException {
        member.delete(ids);
    }

    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws SQLException {
        member.erase(ids);
    }

    @PostMapping("unerase")
    public void unerase(@RequestBody List<Long> ids) throws SQLException {
        member.unerase(ids);
    }
}
